﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Facebook.Schema;
using OpenSocialPlugin.Providers;
using OpenSocialPlugin.General;

namespace OpenSocialPlugin.Providers.Sites.Facebook
{
    public partial class SettingControl : UserControl,ISettingForm
    {
        public SiteSettings settings;
        public SettingControl()
        {
            InitializeComponent();
        }

        private void SettingControl_Load(object sender, EventArgs e)
        {
            settings.Load();
            textBox1.Text = settings.Token;
            checkBox1.Checked = settings.Enabled;
        }


        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                Providers.Facebook face = new Providers.Facebook();
                face.Init();
                face.Service.ConnectToFacebook();
                if (face.Service.SessionExpires)
                {
                  face.Service.ConnectToFacebook(new List<Enums.ExtendedPermissions>() { Enums.ExtendedPermissions.manage_mailbox, Enums.ExtendedPermissions.status_update, Enums.ExtendedPermissions.read_stream, Enums.ExtendedPermissions.publish_stream, Enums.ExtendedPermissions.offline_access });
                    //face.Service.GetExtendedPermission(Types.Enums.Extended_Permissions.offline_access);
                }
                settings.Token = face.Service.Api.AuthToken;
                settings.Uid = face.Service.uid.ToString();
                settings.Secret = face.Service.Api.Session.SessionSecret;
                settings.Session = face.Service.SessionKey;
                textBox1.Text = settings.Session;
            }
            catch
            {
                MessageBox.Show("Incorect login. Try again !!!!");
            }
        }

        #region ISettingForm Members

        public void Save()
        {
            settings.Token = textBox1.Text;
            settings.Enabled = checkBox1.Checked;
            settings.Save();
        }

        #endregion
    }
}
